export const arrayToTree = (arr) => {
  const map = {};
  const tree = [];

  arr.forEach(item => {
    map[item.id] = item;
    item.children = [];
  });
 
  // 构建树结构
  arr.forEach(item => {
    const parent = map[item.parent];
    if (parent) {
      parent.children.push(item);
    } else {
      tree.push(item);
    }
  });
  return tree;
}

export const findLevel = (tree, target, level = 1) => {
  if(target == 'whsw') return level
  for (let i = 0; i < tree.length; i++) {
    if (tree[i].id == target) {
      return level;
    }
    if (tree[i].children && tree[i].children.length > 0) {
      const result = findLevel(tree[i].children, target, level + 1);
      if (result !== undefined) {
        return result;
      }
    }
  }
  return undefined;
}